package cn.iocoder.yudao.module.vlog.comment.controller.admin;

import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.vlog.comment.bo.VLogCommentBO;
import cn.iocoder.yudao.module.vlog.comment.convert.VLogCommentConvert;
import cn.iocoder.yudao.module.vlog.comment.dal.dataobject.VLogCommentDO;
import cn.iocoder.yudao.module.vlog.comment.service.VLogCommentService;
import cn.iocoder.yudao.module.vlog.comment.vo.VLogCommentPageReqVO;
import cn.iocoder.yudao.module.vlog.comment.vo.VLogCommentRespVO;
import cn.iocoder.yudao.module.vlog.comment.vo.VLogCommentVisibleUpdateReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 视频评论")
@RestController
@RequestMapping("/vlog/comment")
@Validated
public class VLogCommentController {

    @Resource
    private VLogCommentService commentService;
    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/update-visible")
    @Operation(summary = "设置视频评论显示/隐藏")
    @PreAuthorize("@ss.hasPermission('vlog:comment:update')")
    public CommonResult<Boolean> updateVLogCommentVisible(@Valid @RequestBody VLogCommentVisibleUpdateReqVO updateReqVO) {
        commentService.updateVLogCommentVisible(updateReqVO.getId(), updateReqVO.getVisible());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得视频评论详情")
    @PreAuthorize("@ss.hasPermission('vlog:comment:query')")
    public CommonResult<VLogCommentRespVO> getVLogComment(Long id) {
        VLogCommentDO commentDO = commentService.getVLogComment(id);
        return success(BeanUtils.toBean(commentDO, VLogCommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得视频评论分页")
    @PreAuthorize("@ss.hasPermission('vlog:comment:query')")
    public CommonResult<PageResult<VLogCommentRespVO>> getCommentPage(@Valid VLogCommentPageReqVO pageReqVO) {
        PageResult<VLogCommentBO> pageResult = commentService.getVLogCommentPage(pageReqVO);
        Set<Long> userIds = convertSet(pageResult.getList(), VLogCommentBO::getUserId);
        Set<Long> toUerIds = convertSet(pageResult.getList(), VLogCommentBO::getToUserId);
        Set<Long> mergeUserIds = new HashSet<>();
        mergeUserIds.addAll(userIds);
        mergeUserIds.addAll(toUerIds);
        List<UserSimpleVO> users = memberUserApi.getUserSimpleList(mergeUserIds);
        return success(VLogCommentConvert.INSTANCE.convertPage(pageResult, users));
    }

    @GetMapping("/liked/list-user")
    @Operation(summary = "获得视频评论点赞的用户列表")
    @Parameter(name = "commentId", description = "视频评论编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vlog:comment:query')")
    public CommonResult<List<UserSimpleVO>> getUserList(Long commentId) {
        Set<Long> userIds = commentService.getVLogCommentLikedUserIds(commentId);
        return success(memberUserApi.getUserSimpleList(userIds));
    }

}
